package com.ldu.houserent.controller.backend;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ldu.houserent.common.base.BaseController;
import com.ldu.houserent.common.constant.Constant;
import com.ldu.houserent.common.dto.JsonResult;
import com.ldu.houserent.common.enums.UserStatusEnum;
import com.ldu.houserent.common.util.PageUtil;
import com.ldu.houserent.controller.front.LoginController;
import com.ldu.houserent.entity.User;
import com.ldu.houserent.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Objects;

/**
 * 用户管理控制器
 */
@Controller
public class UserController extends BaseController {

    @Autowired
    private UserService userService;

    /*
        用户列表
     */
    @RequestMapping("/admin/user")
    public String user(@RequestParam(value = "page", defaultValue = "1") Long pageNumber, @RequestParam(value = "size", defaultValue = "6") Long pageSize, Model model) {
        //封装page
        Page page = PageUtil.initMpPage(pageNumber, pageSize);
        //进行查询
        User condition = new User();
        Page<User> userPage = userService.findAll(page,condition);
        //model存储数据返回前端
        model.addAttribute("pageInfo", userPage);
        model.addAttribute("pagePrefix", "/admin/user?");
        model.addAttribute("tab", "user-list");         //鼠标点击后，改变页面中的字体颜色样式
        return "admin/user-list";
    }

    /*
        禁用用户
     */
    @RequestMapping("/admin/user/disable")
    @ResponseBody
    public JsonResult disable(@RequestParam("id")Long id){
        try {
            User user = userService.get(id);   //根据id获取到用户
            if (user==null){
                return JsonResult.error("操作失败，没有找到该用户");
            }
            user.setStatus(UserStatusEnum.DISABLE.getValue());      //设置用户状态为disable
            userService.update(user);           //更新当前user的信息
        }catch (Exception e){
            return JsonResult.error("禁用用户失败");
        }
        return JsonResult.success("禁用用户成功");
    }

    /*
        启用用户
     */
    @RequestMapping("/admin/user/enable")
    @ResponseBody
    public JsonResult enable(@RequestParam("id")Long id){
        try {
            User user = userService.get(id);   //根据id获取到用户
            if (user==null){
                return JsonResult.error("操作失败，没有找到该用户");
            }
            user.setStatus(UserStatusEnum.ENABLE.getValue());      //设置用户状态为enable
            userService.update(user);           //更新当前user的信息
        }catch (Exception e){
            return JsonResult.error("启用用户失败");
        }
        return JsonResult.success("启用用户成功");
    }

    /*
        进入修改密码页面
     */
    @RequestMapping("/admin/password")
    public String password(Model model) {   //model存储数据返回前端
        model.addAttribute("tab", "password");         //鼠标点击后，改变页面中的字体颜色样式
        return "admin/password";
    }

    /*
        修改密码
     */
    @RequestMapping("/admin/password/submit")
    @ResponseBody
    public JsonResult passwordSubmit(@RequestParam("oldPassword")String oldPassword,
                                     @RequestParam("newPassword")String newPassword,
                                     @RequestParam("confirmPassword")String confirmPassword, HttpSession session){
        if(newPassword==null || newPassword.length()<6){
            return JsonResult.error("新密码不能为空且不少于6位");
        }
        if (!Objects.equals(newPassword,confirmPassword)){
            return JsonResult.error("两次输入的新密码不相同，请重新输入");
        }
        User user = userService.get(getLoginUserId());      //获取到当前用户
        if (user==null || !Objects.equals(user.getUserPass(),oldPassword)){
            return JsonResult.error("旧密码错误，请重新输入");
        }
        user.setUserPass(newPassword);      //设置新密码
        userService.update(user);           //更新数据库中的数据
        session.setAttribute(Constant.SESSION_USER_KEY,userService.get(getLoginUserId()));        //把当前的用户信息存放到session中更新一下
        LoginController loginController = new LoginController();
        loginController.logout(session);
        return JsonResult.success("修改密码成功，请重新登录");
    }
}
